package com.maimang.platform.gen.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.config.Global;
import com.maimang.platform.common.utils.DateUtils;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.gen.entity.TableInfo;
import com.maimang.platform.gen.service.GenService;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.page.TableDataInfo;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname GenController
 * @description 代码生成器
 * @date 2019-04-23 15:20
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/tool/gen")
public class GenController extends BaseController {

	private String prefix = "tool/gen";

	@Autowired
	private GenService genService;

	@RequiresPermissions("tool:gen:view")
	@GetMapping()
	public String gen() {
		return prefix + "/gen";
	}

	/**
	 * 查询数据库表信息
	 *
	 * @param tableInfo
	 * @return
	 */
	@RequiresPermissions("tool:gen:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo getList(TableInfo tableInfo) {
		startPage();
		List<TableInfo> list = genService.selectTableList(tableInfo);
		return getDataTable(list);
	}


	/**
	 * 代码生成
	 *
	 * @param tableName 表名
	 * @param response
	 * @throws IOException
	 */
	@RequiresPermissions("tool:gen:code")
	@Log(title = "代码生成", businessType = BusinessType.GENCODE)
	@GetMapping("/genCode/{tableName}")
	public void generatorCode(@PathVariable("tableName") String tableName, HttpServletResponse response) throws IOException {
		byte[] data = genService.generatorCode(tableName);
		downloadFiles(data, response);
	}


	/**
	 * 批量生成代码
	 *
	 * @param tables   多个表名
	 * @param response
	 * @throws IOException
	 */
	@RequiresPermissions("tool:gen:code")
	@Log(title = "代码生成", businessType = BusinessType.GENCODE)
	@GetMapping("/batchGenCode")
	@ResponseBody
	public void batchGeneratorCode(String tables, HttpServletResponse response) throws IOException {
		String[] tableNames = ConvertUtils.toStrArray(tables);
		byte[] data = genService.generatorCode(tableNames);
		downloadFiles(data, response);
	}

	private void downloadFiles(byte[] data, HttpServletResponse response) throws IOException {
		response.reset();
		String fileName = StringUtils.format("attachment; filename={}.zip", DateUtils.dateTimeNow());
		response.setHeader("Content-Disposition", fileName);
		response.setHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IOUtils.write(data, response.getOutputStream());
	}


	/**
	 * 修改代码属性
	 *
	 * @param modelMap 参数属性
	 * @return
	 */
	@GetMapping("/modify")
	public String modify(ModelMap modelMap) {
		Map<String, Object> property = new HashMap<>(16);
		property.put("author", Global.getAuthor());
		property.put("email", Global.getEmail());
		property.put("package", Global.getPackageName());
		property.put("module", Global.getModuleName());
		property.put("autoRemovePre", Global.getAutoRemovePre());
		property.put("tablePrefix", Global.getTablePrefix());
		modelMap.put("property", property);
		return prefix + "/modify";
	}


	/**
	 * 修改保存代码生成
	 *
	 * @param map
	 * @return
	 */
	@RequiresPermissions("tool:gen:modify")
	@Log(title = "修改保存代码生成", businessType = BusinessType.UPDATE)
	@PostMapping("/modify")
	@ResponseBody
	public Result modifySave(@RequestParam Map<String, Object> map) {
		genService.modifySave(map);
		return success();
	}
}
